<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */
 
class b2c_ctl_admin_goodscollection extends desktop_controller{

    var $workground = 'ectools.workground.order';
    var $certcheck = false;

    function index(){
        $this->finder('b2c_mdl_business_goodscollection',array(
            'title'=>app::get('b2c')->_('集货单'),
	        'allow_detail_popup'=>true,
            'use_buildin_recycle'=>false,
            'use_view_tab'=>true,
            'params'=>array(
                'bill_type' => 'goodscollection',
            )
            ));
    }

    //获取有效订单号 by yyj 2016/4/22
    public function get_bo_ids(){
        $b_order = $this->app->model('business_orders');
        $ids = $b_order->getList('childOrder_id',array('order_status|noequal'=>'dead','status|noequal'=>'0'));
        $order_ids = array();
        foreach($ids as $key => $id){
            $order_ids[] = $id['childOrder_id'];
        }
        return $order_ids;
    }

    /**
     * 集货单view 列表
     * @param null
     * @return null
     */
    public function _views(){
        $mdl_delivery = $this->app->model('business_goodscollection');
        $order_ids = $this->get_bo_ids();
        $sub_menu = array(
            0=>array('label'=>app::get('b2c')->_('未集货'),'optional'=>false,'filter'=>array('status'=>array('0'),'childOrder_id|in'=>$order_ids)),
            1=>array('label'=>app::get('b2c')->_('部分集货'),'optional'=>false,'filter'=>array('status'=>array('1'),'childOrder_id|in'=>$order_ids)),
            2=>array('label'=>app::get('b2c')->_('已集货'),'optional'=>false,'filter'=>array('status'=>array('2'))),
        );

        if(isset($_GET['optional_view'])) $sub_menu[$_GET['optional_view']]['optional'] = false;

        foreach($sub_menu as $k=>$v){
            if($v['optional']==false){
                $show_menu[$k] = $v;
                if(is_array($v['filter'])){
                    $v['filter'] = array_merge(array(),$v['filter']);
                }else{
                    $v['filter'] = array();
                }
                $show_menu[$k]['filter'] = $v['filter']?$v['filter']:null;
                if($k==$_GET['view']){
                    $show_menu[$k]['newcount'] = true;
                    $show_menu[$k]['addon'] = $mdl_delivery->count($v['filter']);
                }
                $show_menu[$k]['href'] = 'index.php?app=b2c&ctl=admin_goodscollection&act=index&view='.($k).(isset($_GET['optional_view'])?'&optional_view='.$_GET['optional_view'].'&view_from=dashboard':'');
            }elseif(($_GET['view_from']=='dashboard')&&$k==$_GET['view']){
                $show_menu[$k] = $v;
            }
        }
        return $show_menu;
    }

    function addnew(){
        echo __FILE__.':'.__LINE__;
    }

	function view($gc_id=null){
		$db = kernel::database();
		$sql = "SELECT g.gc_id, g.memo, o.* FROM sdb_b2c_business_goodscollection AS g
				LEFT JOIN sdb_b2c_business_orders_objects AS o ON g.childOrder_id=o.childOrder_id
				WHERE g.gc_id={$gc_id}";
		$res = $db->select($sql);

		$this->pagedata['info'] = $res;
		$this->pagedata['gc_id'] = $gc_id;
		$this->display('admin/business/gc_view.html');
	}

	function edit($gc_id=null){
		$db = kernel::database();
		$sql = "SELECT g.gc_id, g.memo, o.* FROM sdb_b2c_business_goodscollection AS g
				LEFT JOIN sdb_b2c_business_orders_objects AS o ON g.childOrder_id=o.childOrder_id
				WHERE g.gc_id={$gc_id} AND o.has_confirm_num!=0";
		$res = $db->select($sql);

		$this->pagedata['info'] = $res;
		$this->pagedata['gc_id'] = $gc_id;
		$this->display('admin/business/goodscollection.html');
	}

	function save(){

		$this->begin();
		$obj_boo = $this->app->model('business_orders_objects');
		$obj_bg = $this->app->model('business_goodscollection');
		$obj_order = $this->app->model('orders');
		$obj_order_items = $this->app->model('order_items');
		if($_POST['data']){
			foreach($_POST['data'] as $k=>$v){
				$data_boo['obj_id'] = $v['obj_id'];
				$data_boo['gc_confirm_num'] = $v['gc_confirm_num'];
				$filter_oi['order_id'] = $_POST['mainOrder_id'];
				$filter_oi['bn'] = $v['bn'];
				$data_oi['gcnum'] = $v['gc_confirm_num'];
				$obj_boo->save($data_boo);
				$obj_order_items->update($data_oi,$filter_oi);
			}
			$data_order['order_id'] = $_POST['mainOrder_id'];
			$data_order['c_status'] = 3;
			$obj_order->save($data_order);

			$data_bg['gc_id'] = $_POST['gc_id'];
			$data_bg['memo'] = $_POST['memo'];
			$data_bg['lastmodify'] = time();
			if($_GET['but']==1){
				$data_bg['status'] = 1;
			}elseif($_GET['but']==2){
				$data_bg['status'] = 2;
			}

			if($obj_bg->save($data_bg)){
				$this->end(true,app::get('b2c')->_('保存成功'));
			}else{
				$this->end(false,app::get('b2c')->_('保存失败'));
			}

		}else{
			$this->end(false,app::get('b2c')->_('保存失败'));
		}

	}

	function close(){
		$this->begin();
		$this->end(true,app::get('b2c')->_(''));
	}

}
